package com.example.team.dao.Imp;

import com.example.team.dao.IPropertyDao;
import com.example.team.entity.Property;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *根据接口操作数据库表的接口规范，对其进行实现
 * 编写者：沈超
 * 时间：2023/10/31
 */
@Component
public class IPropertyDaoImpl implements IPropertyDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int insertProperty(Property property) {
        return jdbcTemplate.update("insert into property(Id, PropertyId, PartId, WorkSchedule, WorkTime )" +
                "value(?,?,?,?,?)"
        ,property.getId()
        ,property.getPropertyId()
        ,property.getPartId()
        ,property.getWorkSchedule()
        ,property.getWorkTime());
    }

    @Override
    public int deleteProperty(Property property) {
        return jdbcTemplate.update("delete from  property "+
        "where PropertyId=?",property.getPropertyId());
    }

    @Override
    public List<Property> getAllProperty(Property property) {
        return jdbcTemplate.query("select " +
                "Id,PropertyId,PartId,WorkSchedule,WorkTime " +
        "from Property",new Object[]{},
                new BeanPropertyRowMapper<>(Property.class));
    }

    @Override
    public int updateProperty(Property property) {
        return jdbcTemplate.update("update property set "+
                "Id=?,PartId=?,WorkSchedule=?,WorkTime=? "+
                "where PropertyId=?"
                ,property.getId()
                ,property.getPartId()
                ,property.getWorkSchedule()
                ,property.getWorkTime()
                ,property.getPropertyId());

    }

    @Override
    public Property getProperty(Property property) {
        List<Property> result=jdbcTemplate.query("select " +
                        "Id,PropertyId,PartId,WorkSchedule,WorkTime " +
                        "from property where Id='"+property.getPropertyId()+"'",
                new Object[]{},
                new BeanPropertyRowMapper<>(Property.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }
    }

